package com.acwing.partition44;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2022/3/27 19:00
 */
public class AC4379两个闹钟_枚举 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
        ss = reader.readLine().split(" ");
        int c = Integer.parseInt(ss[0]), d = Integer.parseInt(ss[1]);
        List<Integer> ls1 = new ArrayList<>(), ls2 = new ArrayList<>();
        for (int i = 0; i <= 10000; i++) {
            ls1.add(b);
            ls2.add(d);
            b += a;
            d += c;
        }
        ls1.retainAll(ls2);
        if (ls1.size() == 0) writer.write("-1\n");
        else writer.write(ls1.get(0) + "\n");
        writer.flush();
    }
}
